package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppActivityOfferOwnerListBinding;
import com.sgb.kjwl.model.entity.transport.OwnerGoodsDetailInfo;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.adapter.transport.CargoManageHomeTabAdapter;
import com.sgb.kjwl.view.ui.fragment.transport.OfferOwnerFragment;
import com.sgb.kjwl.view.ui.fragment.transport.OrderOwnerFragment;
import com.sgb.kjwl.viewmodel.self.logistics.OfferOwnerListModel;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe:    报价（接单）单位列表
 * Author:  yangLei
 * Date:    2021/8/24
 */
public class OfferOwnerListActivity extends AppActivity {
    private AppActivityOfferOwnerListBinding binding;
    private OfferOwnerListModel viewModel;

    //倒计时Bar是否有效
    private boolean isValid = false;
    private String goodsNo;
    private OwnerGoodsDetailInfo sourceDetails;
    //顶部tab
    private CargoManageHomeTabAdapter tabAdapter;
    private boolean isGetOrder;//是否是接单

    public static void start(Context context, String sourceNo, boolean isGetOrder) {
        Intent intent = new Intent(context, OfferOwnerListActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("no", sourceNo);
        intent.putExtra(RouterKey.BOOLEAN, isGetOrder);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        goodsNo = getIntent().getStringExtra("no");
        isGetOrder = getIntent().getBooleanExtra(RouterKey.BOOLEAN, false);
        binding = DataBindingUtil.setContentView(OfferOwnerListActivity.this, R.layout.app_activity_offer_owner_list);
        viewModel = new ViewModelProvider(this).get(OfferOwnerListModel.class);
        binding.ivBack.setOnClickListener(view -> finish());
        binding.closeAlter.setOnClickListener(this::onClick);
        binding.rightImageView.setOnClickListener(this);
        initTab();
        initEvent();

        if (!TextUtils.isEmpty(goodsNo)) {
            viewModel.getOwnerGoodsDetailInfo(this, goodsNo);
        } else {
            finish();
        }

        viewModel.detailsLiveData.observe(this, ownerGoodsDetailInfo -> checkLoadingGoodsTime(ownerGoodsDetailInfo));
    }

    private void initTab() {
        List<String> tabs = new ArrayList<>();
        tabs.add("报价单位");
        tabs.add("接单单位");
        tabAdapter = new CargoManageHomeTabAdapter(tabs);
        binding.rvTab.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        binding.rvTab.setAdapter(tabAdapter);
    }

    private void initPager() {
        //设置加载当前页面左右两页内容
        binding.viewpager.setOffscreenPageLimit(1);
        //viewPager适配器
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(OfferOwnerFragment.newInstance(sourceDetails));
        fragments.add(OrderOwnerFragment.newInstance(sourceDetails));
        FragPagerAdapter pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), fragments);
        binding.viewpager.setAdapter(pagerAdapter);

        if (isGetOrder) {
            tabAdapter.setSelectIndex(1);
            binding.viewpager.setCurrentItem(1);
        }
    }

    private void initEvent() {
        binding.ivBack.setOnClickListener(v -> finish());
        tabAdapter.setOnItemClickListener((adapter, view, position) -> {
            tabAdapter.setSelectIndex(position);
            binding.viewpager.setCurrentItem(position);
        });
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabAdapter.setSelectIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    /**
     * 请求到货源信息并进行页面初始化
     */
    private void checkLoadingGoodsTime(OwnerGoodsDetailInfo sourceDetail) {
        if (sourceDetail == null)
            return;
        sourceDetails = sourceDetail;
        initPager();

        //确认为竞价货源
//        if (sourceDetail.getPattern() != null && sourceDetail.getPattern() == 1) {
//            //判断竞价货源是否已过装货时间
//            //以货源装货结束时间作为处理时限
//            if (sourceDetail.getShipmentEndTime() != null) {
//                if (sourceDetail.getShipmentEndTime() > System.currentTimeMillis() && !TextUtils.isEmpty(sourceDetail.getUserNo())
//                        && UserOperating.getInstance().getUserNo().equals(sourceDetail.getUserNo())) {
//                    //装货时间未结束
//                    isValid = true;
//                    binding.countdownDate.setText("您需要在" + DateUtils.formatDate(String.valueOf(sourceDetail.getShipmentEndTime()), "yyyy年MM月dd日") + "前处理");
//                    binding.alterBar.setVisibility(View.VISIBLE);
//                } else {
//                    isValid = false;
//                    binding.alterBar.setVisibility(View.GONE);
//                }
//
//            } else if (sourceDetail.getShipmentTime() != null
//                    && !TextUtils.isEmpty(sourceDetail.getUserNo())
//                    && UserOperating.getInstance().getUserNo().equals(sourceDetail.getUserNo())) {
//                if (sourceDetail.getStartPointTime() == 25 && sourceDetail.getShipmentTime() + 24 * 60 * 60 * 1000 > System.currentTimeMillis()) {
//                    //装货时间未结束
//                    isValid = true;
//                    binding.countdownDate.setText("您需要在" + DateUtils.formatDate(String.valueOf(sourceDetail.getShipmentTime() + 24 * 60 * 60 * 1000), "yyyy年MM月dd日") + "前处理");
//                    binding.alterBar.setVisibility(View.VISIBLE);
//                } else if (sourceDetail.getShipmentTime() > System.currentTimeMillis()) {
//                    //装货时间未结束
//                    isValid = true;
//                    binding.countdownDate.setText("您需要在" + DateUtils.formatDate(String.valueOf(sourceDetail.getShipmentTime()), "yyyy年MM月dd日") + "前处理");
//                    binding.alterBar.setVisibility(View.VISIBLE);
//                } else {
//                    isValid = false;
//                    binding.alterBar.setVisibility(View.GONE);
//                }
//            } else {
//                isValid = false;
//                binding.alterBar.setVisibility(View.GONE);
//            }
//        }
    }

    @Override
    public void onMultiClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.right_image_view:
                if (sourceDetails != null && !TextUtils.isEmpty(sourceDetails.getNo()))
                    QuotedOwnerActivity.start(this, sourceDetails.getNo());
                break;

            case R.id.close_alter:
                binding.alterBar.setVisibility(View.GONE);
                break;
        }
    }
}
